
const TDom={
  "createButton":function(parent, text, onclick, id=''){
    const button     = document.createElement('button');
    button.innerText = text;
    button.id        = id;
    button.onclick   = onclick;
    if(parent) parent.appendChild(button);
    return button;
  },
  "createLabel":function(parent, text="", id='' ,forV=""){
    const label     = document.createElement('label');
    label.innerText = text;
    label.id        = id;
    label.setAttribute('for',forV)
    if(parent) parent.appendChild(label);
    return label;
  },
  "createDiv":function(parent, id=''){
    const div     = document.createElement('div');
    div.id        = id;
    if(parent) parent.appendChild(div);
    return div;
  },
  "createNumberInput":function(parent, onchange, value, step=0.1, id=''){
    const input   = document.createElement('input');
    input.type    = 'number';
    input.step    = step;
    input.value   = value;
    input.id      = id;
    input.onchange = onchange;

    if(parent) parent.appendChild(input);
    return input;
  },
  "createFileSelector":function(parent, onchange, id=''){
    const selector   = document.createElement('input');
    selector.type    = 'file';
    selector.id      = id;
    selector.onchange = onchange;
    
    if(parent) parent.appendChild(selector);
    return selector;
  },

  "getDataFromUrl":function(url, callback, type){
    const init ={
      method : "GET",
      mode   : "no-cors",
      headers: {
        'Content-Type': 'text/plain',
      },
    };
    // 根据链接后缀获取文件类型
    if(!type) type = url.slice(url.lastIndexOf(".")+1);

    switch(type){
      case "MD": 
      case "md":
      case "txt": 
      case "dat":
      init.headers["Content-Type"] = 'text/plain';break;
      case "json": init.headers["Content-Type"] = 'application/json';break;
      case "svg" : init.headers["Content-Type"] = 'text/xml';break;
      default:break;
    }
    fetch(url, init).then((response)=>{
      if(response.ok){
        switch(type){
          case "text": return response.text();
          case "json": return response.json();
          case "svg" : return response.text();
          default    : return response.text();
        }
      }
    }).then(callback)
  },
}


export default TDom;